package com.cnoja.jmsncn.command.commands;

import org.apache.log4j.Logger;

import com.cnoja.jmsncn.command.AbstractCommand;
import com.cnoja.jmsncn.command.event.NotificationHostChangeEvent;
import com.cnoja.jmsncn.inf.IResponse;
import com.cnoja.jmsncn.kernel.DefaultMessengerImpl;
import com.cnoja.jmsncn.kernel.exceptions.CommandValidateException;
import com.cnoja.jmsncn.utils.command.CommandUtils;

public class UserCommand extends AbstractCommand {
	private Logger logger = Logger.getLogger(UserCommand.class);

	public UserCommand(String commandString) {
		super(commandString);
	}

	@Override
	public void validateResponse(IResponse response)
			throws CommandValidateException {
		String[] split = CommandUtils.split(response.getResponseContent());
		if (split.length < 3) {
			throw new CommandValidateException("The response has an error:"
					+ split[0]);
		}
		if ("XFR".equals(split[0]) && "NS".equals(split[2])) {
			String host = CommandUtils.extractHost(split[3]);
			int port = CommandUtils.extractPort(split[3]);
			
			this.getCommander().fireCommanderEvent(new NotificationHostChangeEvent(this.getCommander(),this,host,port));


		}else if("USR".equals(split[0])&&"S".equals(split[3])){
//			"ct=1247035442,rver=5.5.4177.0,wp=FS_40SEC_0_COMPACT,lc=1033,id=507,ru=http:%2F%2Fmessenger.msn.com,tw=0,kpp=1,kv=4,ver=2.1.6000.1,rn=1lgjBfIL,tpf=b0735e3a873dfb5e75054465196398e0"
			
			
		}else{
			throw new CommandValidateException("The response is not expected:"+response.getResponseContent());
		}
	}
}
